package com.CityHunter.Activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.CityHunter.R;
import com.CityHunter.Adapter.BuddyAdapter;
import com.CityHunter.Common.Http;
import com.CityHunter.DataObject.Buddy;

public class GroupPage extends Activity {

	private ListView gmbp_lv_groupMemberList;

	private ArrayList<Buddy> buddies;
	private BuddyAdapter adapter;
	private SharedPreferences sp;
	private int groupid, userid;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.gmbp);

		sp = getSharedPreferences("cityhunter", 0);
		userid = sp.getInt("userid", 0);

		// Mapping EditText
		gmbp_lv_groupMemberList = (ListView) findViewById(R.id.gmbp_lv_groupMemberList);

		// Get groupid from Bundle
		Bundle bundle = this.getIntent().getExtras();
		groupid = bundle.getInt("groupid");

		getGroupMember();
	}

	private void getGroupMember() {

		// Declare http objects
		String getGroupMemberPage = getString(R.string.url_getGroupMemberPage);

		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("groupid", Integer.toString(groupid)));
		params.add(new BasicNameValuePair("userid", Integer.toString(userid)));

		// Send Get Buddy Request
		Http http = new Http();
		buddies = new ArrayList<Buddy>();
		Log.d("gmbp_Debug", "Sending Request : " + getGroupMemberPage);
		JSONArray jsonResponse = http.sendRequest(getGroupMemberPage, params);
		try {
			for (int i = 0; i < jsonResponse.length(); i++) {

				int buddyid = Integer.parseInt(jsonResponse.getJSONObject(i)
						.getString("userid"));
				String name = jsonResponse.getJSONObject(i).getString("name");
				int state = Integer.parseInt(jsonResponse.getJSONObject(i)
						.getString("state"));

				Buddy bb = new Buddy(buddyid, name, state);
				buddies.add(bb);

			}
			Log.d("gmbp_Debug", "Response : No of GroupMember = "
					+ jsonResponse.length());
		} catch (Exception e) {
			Log.e("bdyp_Error", e.getMessage().toString());
		}

		adapter = new BuddyAdapter(this, buddies);
		// Add to ListView
		gmbp_lv_groupMemberList.setAdapter(adapter);
		gmbp_lv_groupMemberList.setOnItemClickListener(gmlvListener);
	}

	private OnItemClickListener gmlvListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> l, View v, int position, long id) {

			Buddy selectedBuddy = buddies.get(position);

			Log.d("gmbp_Debug",
					"Selected Buddy ID : " + selectedBuddy.getBuddyid());

		}
	};
}
